package com.dailybule.java.mybatis.m3;

import com.dailybule.java.mybatis.m3.Mapper.EmpMapper;
import com.dailybule.java.mybatis.m3.bean.Emp;
import lombok.SneakyThrows;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

public class Main {

    @SneakyThrows
    private static void a() {
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setJob("程序员");
        emp.setEnname("张");
        List<Emp> list = mapper.find1(emp);
        list.forEach(System.out::println);
        session.close();

    }

    @SneakyThrows
    private static void b() {
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        //emp.setJob("程序员");
        //emp.setEname("张");
        List<Emp> list = mapper.find2(emp);
        list.forEach(System.out::println);
        session.close();

    }

    @SneakyThrows
    private static void c() {
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setJob("程序员");
        //emp.setEname("张");
        List<Emp> list = mapper.find3(emp);
        list.forEach(System.out::println);
        session.close();
    }

    @SneakyThrows
    private static void d() {
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setSal(9999.0);
        List<Emp> list = mapper.find4(emp);
        list.forEach(System.out::println);
        session.close();
    }

    @SneakyThrows
    private static void e(){
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        String[] array = {"程序员","网页设计","歌手","MANAGER"};
        List<Emp> list = mapper.find5(array);
        list.forEach(System.out::println);
        session.close();
    }

    @SneakyThrows
    private static void f(){
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        List<Emp> list = mapper.find6(Arrays.asList("程序员","网页设计","歌手","MANAGER"));
        list.forEach(System.out::println);
        session.close();
    }

    @SneakyThrows
    private static void g(){
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setEnname("a");
        List<Emp> list = mapper.find7(emp);
        list.forEach(System.out::println);
        session.close();
    }

    @SneakyThrows
    private static void h(){
        InputStream in = Resources.getResourceAsStream("config.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
        SqlSession session = ssf.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setEnname("a");
        emp.setJob("bbb");
        emp.setEmpno(7988);
        mapper.updateEmp(emp);
        session.close();
    }
    public static void main(String[] args) {
        h();
    }
}